package com.xqls.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;

import com.xqls.dal.dao.TScheduleJobDao;
import com.xqls.dal.model.TScheduleJob;
import com.xqls.service.QuatzOperationService;
public class QuatzOperationServiceImpl implements QuatzOperationService {
	@Autowired
	private SchedulerFactoryBean schedulerFactoryBean;
	@Autowired
	private TScheduleJobDao tScheduleJobDao;
	@Override
	public List<TScheduleJob> getAllJob()  throws SchedulerException{
		List<TScheduleJob> jobList = tScheduleJobDao.selectAllJob();
		return jobList;
	}
	  /** 
     * 所有正在运行的job 
     *  
     * @return 
     * @throws SchedulerException 
     */ 
	@Override
	public List<TScheduleJob> getRunningJob() throws SchedulerException {
		 Scheduler scheduler = schedulerFactoryBean.getScheduler();  
	        List<JobExecutionContext> executingJobs = scheduler  
	                .getCurrentlyExecutingJobs();  
	        List<TScheduleJob> jobList = new ArrayList<TScheduleJob>(  
	                executingJobs.size());
	        for (JobExecutionContext executingJob : executingJobs) {  
	            TScheduleJob job = new TScheduleJob();  
	            JobDetail jobDetail = executingJob.getJobDetail();  
	            JobKey jobKey = jobDetail.getKey();  
	            Trigger trigger = executingJob.getTrigger();  
	            job.setfJobName(jobKey.getName());  
	            job.setfJobGroup(jobKey.getGroup());  
	            job.setfDescription("触发器:" + trigger.getKey());  
	            Trigger.TriggerState triggerState = scheduler  
	                    .getTriggerState(trigger.getKey());  
	            job.setfJobStatus(triggerState.name());  
	            if (trigger instanceof CronTrigger) {  
	                CronTrigger cronTrigger = (CronTrigger) trigger;  
	                String cronExpression = cronTrigger.getCronExpression();  
	                job.setfCronExpression(cronExpression);  
	            }  
	            jobList.add(job);  
	        }  
		return jobList;
	}
	  /** 
     * 暂停一个job 
     *  
     * @param TTScheduleJob 
     * @throws SchedulerException 
     */ 
	@Override
	public int upatePauseJob(TScheduleJob scheduleJob) throws SchedulerException{
		 /*int i = tScheduleJobDao.updateById(scheduleJob);  
	        if(i > 0){  
	            Scheduler scheduler = schedulerFactoryBean.getScheduler();  
	            JobKey jobKey = JobKey.jobKey(scheduleJob.getfJobName(),  
	                    scheduleJob.getfJobGroup());  
	            scheduler.pauseJob(jobKey);  
	        }  */
		return 0;
	}
	  /** 
     * 恢复一个job 
     *  
     * @param scheduleJob 
     * @throws SchedulerException 
     */ 
	@Override
	public void resumeJob(TScheduleJob scheduleJob) throws SchedulerException {
		 Scheduler scheduler = schedulerFactoryBean.getScheduler();  
	        JobKey jobKey = JobKey.jobKey(scheduleJob.getfJobName(),  
	                scheduleJob.getfJobGroup());  
	        scheduler.resumeJob(jobKey);  

	}
    /** 
     * 删除一个job 
     *  
     * @param scheduleJob 
     *  
     * @throws SchedulerException 
     */ 
	@Override
	public void deleteJob(TScheduleJob scheduleJob) throws SchedulerException {
		Scheduler scheduler = schedulerFactoryBean.getScheduler();  
        JobKey jobKey = JobKey.jobKey(scheduleJob.getfJobName(),  
                scheduleJob.getfJobGroup());  
        scheduler.deleteJob(jobKey);  

	}
	/** 
     * 立即执行job 
     *  
     * @param scheduleJob 
     * @throws SchedulerException 
     */  
	@Override
	public void runAJobNow(TScheduleJob scheduleJob)  throws SchedulerException{
		Scheduler scheduler = schedulerFactoryBean.getScheduler();  
        JobKey jobKey = JobKey.jobKey(scheduleJob.getfJobName(),  
                scheduleJob.getfJobGroup());  
        scheduler.triggerJob(jobKey);

	}
	/** 
     * 更新job时间表达式 
     *  
     * @param scheduleJob 
     *  
     * @throws SchedulerException 
     */  
	@Override
	public void updateJobCron(TScheduleJob scheduleJob)  throws SchedulerException{
		 Scheduler scheduler = schedulerFactoryBean.getScheduler();  
		  
	        TriggerKey triggerKey = TriggerKey.triggerKey(  
	                scheduleJob.getfJobName(), scheduleJob.getfJobGroup());  
	  
	        CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);  
	  
	        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder  
	                .cronSchedule(scheduleJob.getfCronExpression());  
	  
	        trigger = trigger.getTriggerBuilder().withIdentity(triggerKey)  
	                .withSchedule(scheduleBuilder).build();  
	  
	        scheduler.rescheduleJob(triggerKey, trigger);  

	}

}
